<!DOCTYPE html>
<html>
<!--
Copyright 2012 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.labs.async.transform</title>
<script src="../../base.js"></script>
<script>

goog.require('goog.Timer');
goog.require('goog.labs.async.ResultBase');
goog.require('goog.labs.async.transform');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');

</script>
</head>
<body>
<script>

var result, resultCallback, multiplyResult, mockClock;

function setUpPage() {
  mockClock = new goog.testing.MockClock();
  mockClock.install();
}

function setUp() {
  mockClock.reset();
  result = new goog.labs.async.ResultBase();
  resultCallback = new goog.testing.recordFunction();
  multiplyResult = goog.testing.recordFunction(function(value) {
      return value * 2;
    });
}

function tearDown() {
  result = multiplyResult = null;
}

function tearDownPage() {
  mockClock.uninstall();
  goog.dispose(mockClock);
}

function testTransformWhenResultSuccess() {
  var transformedResult = goog.labs.async.transform(result, multiplyResult);
  goog.labs.async.wait(transformedResult, resultCallback);

  assertEquals(goog.labs.async.Result.State.PENDING, result.getState());
  result.setValue(1);
  assertTransformerCall(multiplyResult, 1);
  assertSuccessCall(resultCallback, transformedResult, 2);
}

function testTransformWhenResultSuccessAsync() {
  var transformedResult = goog.labs.async.transform(result, multiplyResult);
  goog.labs.async.wait(transformedResult, resultCallback);

  goog.Timer.callOnce(function() {
    result.setValue(1);
  });

  assertEquals(goog.labs.async.Result.State.PENDING, result.getState());
  mockClock.tick();
  assertTransformerCall(multiplyResult, 1);
  assertSuccessCall(resultCallback, transformedResult, 2);
}

function testTransformWhenResultError() {
  var transformedResult = goog.labs.async.transform(result, multiplyResult);
  goog.labs.async.wait(transformedResult, resultCallback);

  assertEquals(goog.labs.async.Result.State.PENDING, result.getState());
  result.setError();
  assertNoCall(multiplyResult);
  assertErrorCall(resultCallback, transformedResult);
}

function testTransformWhenResultErrorAsync() {
  var transformedResult = goog.labs.async.transform(result, multiplyResult);

  goog.labs.async.wait(transformedResult, resultCallback);

  goog.Timer.callOnce(function() {
    result.setError();
  });

  assertEquals(goog.labs.async.Result.State.PENDING, result.getState());
  mockClock.tick();
  assertNoCall(multiplyResult);
  assertErrorCall(resultCallback, transformedResult);
}

function assertSuccessCall(recordFunction, result, value) {
  assertEquals(1, recordFunction.getCallCount());

  var res = recordFunction.popLastCall().getArgument(0);
  assertEquals(result, res);
  assertEquals(goog.labs.async.Result.State.SUCCESS, res.getState());
  assertEquals(value, res.getValue());
}

function assertErrorCall(recordFunction, result) {
  assertEquals(1, recordFunction.getCallCount());

  var res = recordFunction.popLastCall().getArgument(0);
  assertEquals(result, res);
  assertEquals(goog.labs.async.Result.State.ERROR, res.getState());
}

function assertNoCall(recordFunction) {
  assertEquals(0, recordFunction.getCallCount());
}

function assertTransformerCall(recordFunction, value) {
  assertEquals(1, recordFunction.getCallCount());

  var argValue = recordFunction.popLastCall().getArgument(0);
  assertEquals(value, argValue);
}

</script>
</body>
</html>
